package com.fresh.bus.canoe.xsocket;

import com.fresh.bus.canoe.server.support.AbstractCanoeServer;

import java.io.IOException;
import java.util.concurrent.Executors;

import org.xsocket.connection.IServer;
import org.xsocket.connection.Server;
import org.xsocket.connection.IConnection.FlushMode;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class XSocketCanoeServer extends AbstractCanoeServer {
    private static final Logger log = LoggerFactory.getLogger(XSocketCanoeServer.class);
    private IServer server;
    private int port;

    public XSocketCanoeServer(int port) throws IOException {
        this.port = port;
    }

    @Override
    public void close() throws IOException {
        server.close();
    }

    @Override
    public void start() {
        super.start();

        try {
            CanoeHandler handler = new CanoeHandler();
            handler.setLifecycleHandler(getLifecycleHandler());
            handler.setMessageCodec(getMessageCodec());
            server = new Server(port, handler);
            //server.setAutoflush(false);
           
            server.setIdleTimeoutMillis(30*60*1000);
			server.setWorkerpool(Executors.newFixedThreadPool(500));
			server.setFlushmode(FlushMode.ASYNC);
            server.start();
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
    }
}
